package com.jeeplus.modules.sys.web;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.mapper.JsonMapper;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.sys.entity.*;
import com.jeeplus.modules.sys.service.DataHubService;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.test.tree.dialog.entity.TestTree1;
import com.jeeplus.modules.test.tree.dialog.service.TestTree1Service;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @author Administrator
 * @title: DataHubControl
 * @projectName jeeplus
 * @description: TODO
 * @date 2019/7/2416:41
 */
@Controller
@RequestMapping(value = "${adminPath}/DataHub")
public class DataHubControl extends BaseController {
    @Autowired
    DataHubService dataHubService;
    //region 视图功能
    @RequiresPermissions("report:list")
    @RequestMapping(value = {"list", ""})
    public String list(ServiceInfo serviceInfo, Model model) {
        model.addAttribute("serviceInfo", serviceInfo);
        return "modules/DataHub/ServiceInfo";
    }
    @RequiresPermissions("report:list")
    @RequestMapping(value = {"interfaceList", ""})
    public String interfaceList(InterfaceList interfaceList, Model model){
        model.addAttribute("interfaceList",interfaceList);
        return "modules/DataHub/InterfaceList";
    }
    /**
     * 查看，增加，编辑国家表单页面
     */
    @RequiresPermissions(value={"DataHub:view","DataHub:add","DataHub:edit"},logical= Logical.OR)
    @RequestMapping(value = "form")
    public String form(ServiceInfo serviceInfo, @RequestParam(required=false) String id,Model model,HttpServletRequest request) {
        //System.out.println(request.getParameter("platform.id"));
        TestTree1 testTree1=new TestTree1();
        testTree1.setValue("Linux1");
        //System.out.println(testTree1Service.get(testTree1));
        serviceInfo=dataHubService.get(id);
        TestTree1 testTree1Child = new TestTree1();
        testTree1Child.setId(serviceInfo.getPlatform());
        testTree1Child.setParentIds(dataHubService.getTreeRow().get(0).getId());
        testTree1Child.setName(serviceInfo.getPlatform());
        testTree1Child.setValue(serviceInfo.getPlatform());
        serviceInfo.setPlatformInfo(testTree1Child);
        model.addAttribute("testCountry",serviceInfo );
        return "modules/DataHub/ServiceInfoForm";
    }
    @RequiresPermissions(value={"DataHub:view","DataHub:add","DataHub:edit"},logical= Logical.OR)
    @RequestMapping(value = "InterfaceForm")
    public String InterfaceForm(InterfaceList interfaceList,@RequestParam(required = false) String id,Model model,HttpServletRequest request){
        interfaceList=dataHubService.getInterfaceList(id);
        model.addAttribute("interfaceList",interfaceList);

        return "modules/DataHub/InterfaceForm";
    }
    //endregion
    //region 基础功能
    protected String getOfficeCode(String officeId)
    {
        List<String> list=new ArrayList<>();
        for (Office off: UserUtils.getOfficeList()){
            for (String p:officeId.split(",")){
                if(off.getId().contains(p)){
                    if(!list.contains(off.getCode())&&!off.getCode().isEmpty())
                    {
                        list.add(off.getCode());
                    }
                }
            }
        }
        return StringUtils.join(list.toArray(),",");
    }
    //endregion
    //region 数据
    @ResponseBody
    @RequiresPermissions("DataHub:list")
    @RequestMapping(value = "data")
    public Map<String, Object> List(ServiceInfo serviceInfo,HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ServiceInfo> page = dataHubService.findPage(new Page<ServiceInfo>(request, response), serviceInfo);
        if(StringUtils.isNotBlank(request.getParameter("office.id")))
        System.out.println(Integer.valueOf(getOfficeCode(serviceInfo.getOffice().getId())));
        return getBootstrapListData(page);
    }
    @ResponseBody
    @RequiresPermissions("DataHub:list")
    @RequestMapping(value = "interfaceListData")
    public Map<String, Object> interfaceListData(InterfaceList interfaceList,HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<InterfaceList> page = dataHubService.findPage(new Page<InterfaceList>(request, response), interfaceList);
//        if(StringUtils.isNotBlank(request.getParameter("office.id")))
//            System.out.println(Integer.valueOf(getOfficeCode(interfaceList.getOffice().getId())));
        return getBootstrapListData(page);
    }
    @Autowired
    private TestTree1Service testTree1Service;
    /**
     * 获取树列表
     *
     * @Author LH
     * @Description //TODO
     * @Date 20:59 2019/7/26
     * @param extId
     * @param response
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
    @RequiresPermissions("user")
    @ResponseBody
    @RequestMapping(value = "treeData")
    public List<Map<String,Object>> treeData(@RequestParam(required=false) String extId,HttpServletResponse response){
        List<Map<String, Object>> mapList = Lists.newArrayList();
        List<TestTree1> parRow=dataHubService.getTreeRow();
        TestTree1 testTree1=new TestTree1();
        testTree1.setParentIds(parRow.get(0).getId());
        List<TestTree1> list =testTree1Service.findList(testTree1);
        list.add(testTree1Service.get(parRow.get(0).getId()));
        for (int i=0; i<list.size(); i++){
            TestTree1 e = list.get(i);
            if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1)){
                Map<String, Object> map = Maps.newHashMap();
                map.put("id", e.getId());
                map.put("text", e.getName());
                map.put("value",e.getValue());
                map.put("labelValue",e.getValue());
                if((StringUtils.isBlank(e.getParentId()) || "0".equals(e.getParentId()))){
                    map.put("parent", "#");
                    Map<String, Object> state = Maps.newHashMap();
                    state.put("opened", true);
                    map.put("state", state);
                }else{
                    map.put("parent", e.getParentId());
                }
                mapList.add(map);
            }

        }
        return mapList;
    }
    //endregion
    //region 提交功能
    /**
     * 保存表单
     *
     * @Author LH
     * @Description //TODO
     * @Date 20:59 2019/7/26
     * @param serviceInfo
     * @param model
     * @return com.jeeplus.common.json.AjaxJson
     **/
    @ResponseBody
    @RequiresPermissions(value={"DataHub:add","DataHub:edit"},logical=Logical.OR)
    @RequestMapping(value = "save")
    public AjaxJson save(ServiceInfo serviceInfo, Model model,HttpServletRequest request) throws Exception{
        AjaxJson j = new AjaxJson();
        String errMsg = beanValidator(serviceInfo);
        //System.out.println(request.getParameter("platform.id"));
        if (StringUtils.isNotBlank(errMsg)){
            j.setSuccess(false);
            j.setMsg(errMsg);
            return j;
        }
        if(request.getParameter("platform.id")!=null)
        serviceInfo.setPlatform(request.getParameter("platform.id"));
        if(dataHubService.save(serviceInfo))
        {
            j.setSuccess(true);
            j.setMsg("保存服务器信息成功");
        }else {
            j.setSuccess(false);
            j.setMsg("保存服务器信息失败");
        }

        return j;
    }
    @ResponseBody
    @RequiresPermissions(value={"DataHub:add","DataHub:edit"},logical=Logical.OR)
    @RequestMapping(value = "saveInterface")
    public AjaxJson saveInterface(InterfaceList interfaceList,Model model,HttpServletRequest request){
        AjaxJson j = new AjaxJson();
        String errMsg = beanValidator(interfaceList);
        if (StringUtils.isNotBlank(errMsg)){
            j.setSuccess(false);
            j.setMsg(errMsg);
            return j;
        }
        if(dataHubService.saveInterfaceList(interfaceList))
        {
            j.setSuccess(true);
            j.setMsg("保存服务器信息成功");
        }else {
            j.setSuccess(false);
            j.setMsg("保存服务器信息失败");
        }
        return j;
    }
    //endregion
}
